package err_msg

import (
	"fmt"
	"github.com/go-playground/validator/v10"
)

// TranslateParamsErr 自定义参数错误信息转换函数
func TranslateParamsErr(err error, validationErrorMessages map[string]string) string {
	if _, ok := err.(*validator.InvalidValidationError); ok {
		return "验证配置错误"
	}

	for _, err := range err.(validator.ValidationErrors) {
		key := fmt.Sprintf("%s.%s", err.Field(), err.Tag())
		if msg, exists := validationErrorMessages[key]; exists {
			return msg
		}
	}

	for _, err := range err.(validator.ValidationErrors) {
		key := fmt.Sprintf("%s.%s", err.Field(), err.Tag())
		if msg, exists := CommonMsg[key]; exists {
			return msg
		}
	}

	return "输入参数有误"
}
